﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Infusion.Silverlight.Controls.Ribbon;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using ASPItalia.ModelVirtualCasting.Web.RiaServices;
using System.Collections;
using System.Reflection;
using System.Windows.Printing;
using System.Runtime.InteropServices.Automation;
using Uploader;

namespace ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.ViewModels
{
  public class MainPageViewModel : ViewModelBase
  {
    public RelayCommand<object> LoadCommand { get; private set; }
    public RelayCommand<object> NewCommand { get; private set; }
    public RelayCommand<object> CopyCommand { get; private set; }
    public RelayCommand<object> PasteCommand { get; private set; }
    public RelayCommand<object> PrintCommand { get; private set; }
    public RelayCommand<object> ExportCommand { get; private set; }
    public RelayCommand<object> SaveCommand { get; private set; }

    public MainPageViewModel()
    {
      Processing = new Tuple<bool, string>(false, string.Empty);

      LoadCommand = new RelayCommand<object>(Load, CanLoad);
      NewCommand = new RelayCommand<object>(New, CanNew);
      CopyCommand = new RelayCommand<object>(Copy, CanCopy);
      PasteCommand = new RelayCommand<object>(Paste, CanPaste);
      PrintCommand = new RelayCommand<object>(Print, CanPrint);
      ExportCommand = new RelayCommand<object>(Export, CanExport);
      SaveCommand = new RelayCommand<object>(Save, CanSave);

      Messenger.Default.Register<Tuple<bool, string>>(this, (m) => { Processing = m; });

      Messenger.Default.Register<IList>(this, (items) =>
      {
        Items.Clear();
        foreach (People item in items)
          Items.Add(item);

        CopyCommand.RaiseCanExecuteChanged();
      });

      _menuItems.Add(new RibbonTab()
      {
        Title = "Home"
        ,
        Groups = {
          new RibbonTabGroup(){ 
            Title="Risorse" 
            ,
            Items= {
               new RibbonButton(){ Text = "Carica",LargeImageSource = "Images/Reload.png",Command = LoadCommand}
              ,new RibbonButton(){ Text = "Nuovo",LargeImageSource = "Images/New.png",Command = NewCommand}
              ,new RibbonButton(){ Text = "Copia",LargeImageSource = "Images/Copy.png",Command = CopyCommand}
              ,new RibbonButton(){ Text = "Incolla",LargeImageSource = "Images/Paste.png",Command = PasteCommand}
              ,new RibbonButton(){ Text = "Stampa",LargeImageSource = "Images/Print.png",Command = PrintCommand}
              ,new RibbonButton(){ Text = "Esporta",LargeImageSource = "Images/Export.png",Command = ExportCommand}
              ,new RibbonButton(){ Text = "Salva",LargeImageSource = "Images/Save.png",Command = SaveCommand}
            }
          }
        }
      });
    }

    private RibbonTabCollection _menuItems = new RibbonTabCollection();
    public RibbonTabCollection MenuItems
    {
      get { return _menuItems; }
      set
      {
        _menuItems = value;
        RaisePropertyChanged("MenuItems");
      }
    }

    private ObservableCollection<People> _items = new ObservableCollection<People>();
    public ObservableCollection<People> Items
    {
      get { return _items; }
      set
      {
        _items = value;
        RaisePropertyChanged("Items");
      }
    }

    Tuple<bool, string> _processing;
    public Tuple<bool, string> Processing
    {
      get { return _processing; }
      set
      {
        _processing = value;
        RaisePropertyChanged("Processing");
      }
    }

    public void Load(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Load); }
    public bool CanLoad(object parameter) { return !Processing.Item1; }

    public void New(object parameter) { }
    public bool CanNew(object parameter) { return true; }

    public void Copy(object parameter)
    {
      string outData = string.Empty;

      foreach (People item in Items)
      {
        outData += ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.Manager.Helper.ObjectToString(item);
      }

      Clipboard.SetText(outData);
    }

    public bool CanCopy(object parameter) { return Items.Count >= 1; }

    public void Paste(object parameter) { }
    public bool CanPaste(object parameter) { return true; }

    public void Print(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Print); }
    public bool CanPrint(object parameter) { return true; }

    public void Export(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Export); }
    public bool CanExport(object parameter)
    {
      return (AutomationFactory.IsAvailable && Application.Current.HasElevatedPermissions);
    }

    public void Save(object parameter) { Messenger.Default.Send<MessageType>(MessageType.Save); }
    public bool CanSave(object parameter)
    {
      return (AutomationFactory.IsAvailable && Application.Current.HasElevatedPermissions);
    }
  }
}
